import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { useRoute, useRouter } from "vue-router";

import * as dd from "dingtalk-jsapi";

const storeSetup = () => {
  /* 定义数据--------------------------------------------------------------------- */
  const userInfo = ref({
    userName: "王伟伦",
    avatar: null,
  });

  // 免登授权码
  const authCode = ref("");

  /* 定义函数--------------------------------------------------------------------- */
  // 登录
  const login = () => {
    if (isDingTalk()) {
      getAuthCode();
    }
  };

  // 判断是否是钉钉容器(忽略开发模式)
  const isDingTalk = () => {
    return !dd.env.platform === "notInDingTalk";
  };

  // 获取免登授权码
  const getAuthCode = () => {
    dd.requestAuthCode({
      corpId: "",
      clientId: "",
      onSuccess: function (res) {
        authCode.value = res.code;
      },
      onFail: function () {},
    });
  };

  // 返回参数
  return {
    userInfo,
    isDingTalk,
    login,
  };
};

// 导出Store
export const useUserStore = defineStore("user", storeSetup, { persist: true });
